package com.edu.admin.service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edu.admin.common.BaseService;
import com.edu.admin.entity.SysRole;
import com.edu.admin.entity.SysRoleMenu;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.enums.UseTypeEnum;
import com.edu.admin.exception.SysException;
import com.edu.admin.mapper.RoleMapper;
import com.edu.admin.vo.RoleVo;
import com.edu.admin.vo.RoleMenuVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 角色服务
 *
 * @author Wangjiong
 * 
 */
@Transactional
@Service
public class RoleService extends BaseService<RoleMapper, SysRole> {
	@Autowired
	RoleMenuService roleMenuService;
	
	/**
	 * 获取可用角色列表
	 * 
	 * @param 
	 * @return List<RoleVo>
	 */
	public List<RoleVo> getRoleList() {
		List<RoleVo> roleList = this.mapper.getRoleList();
		for (RoleVo roleVo : roleList)
		{
			List<RoleMenuVo> roleMenuList = roleMenuService.getRoleMenuListByRoleId(roleVo.getRoleId());
			Long[] menuIds = new Long[roleMenuList.size()];
			int i =0 ;
			for (RoleMenuVo roleMenuVo : roleMenuList)
			{
				menuIds[i] = roleMenuVo.getMenuId();
				i++;
			}
			roleVo.setMenuIds(menuIds);
		}
		return roleList;
	}
	
	/**
	 * 获取所有角色列表
	 * 
	 * @param 
	 * @return List<RoleVo>
	 */
	public List<RoleVo> getAllRoleList() {
		List<RoleVo> roleList = this.mapper.getAllRoleList();
		for (RoleVo roleVo : roleList)
		{
			List<RoleMenuVo> roleMenuList = roleMenuService.getRoleMenuListByRoleId(roleVo.getRoleId());
			Long[] menuIds = new Long[roleMenuList.size()];
			int i =0 ;
			for (RoleMenuVo roleMenuVo : roleMenuList)
			{
				menuIds[i] = roleMenuVo.getMenuId();
				i++;
			}
			roleVo.setMenuIds(menuIds);
		}
		return roleList;
	}
	
	/**
	 * 获取所有角色列表（分页）
	 * 
	 * @param 
	 * @return PageInfo<RoleVo>
	 */
	public PageInfo<RoleVo> getAllRoleListPage(Integer pageNum, Integer pageSize) {
		
		if (pageNum == null) {
			pageNum = 1;
		}
		if (pageSize == null) {
			pageSize = 10;
		}
		PageHelper.startPage(pageNum, pageSize);
		
		List<RoleVo> roleList = getAllRoleList();
		
		PageInfo<RoleVo> pageInfo = new PageInfo<RoleVo>(roleList);
		
		return pageInfo;
	}
	
	/**
     * 角色重复性检查
     * @param SysRole role
     * 
     * @return List<RoleVo>
     * @throws Exception
     */
    public List<RoleVo> checkRole(SysRole role) throws Exception {
    	
    	return this.mapper.findRoleByRoleName(role.getRoleId(), role.getRoleName().trim());
  
    }
	
	
	/**
	 * 角色保存
	 * 
	 * @param SysRole
	 * 
	 * @return Map<String, Object>
	 * @throws Exception 
	 */
	public Map<String, Object> saveRole(SysRole role) throws Exception {
		Map<String, Object> result = new HashMap<>();
		
		List<RoleVo> roleList = checkRole(role);
		
		if (roleList!=null && roleList.size()>0)
		{
			throw new SysException(ExceptionEnum.ROLENAMEREPEAT);
		}
		
		Long roleId = role.getRoleId();

		if (roleId == null) {
			
			role.setUseType(UseTypeEnum.ISUSE.getCode());
			
			insert(role);
			
			roleId = role.getRoleId();

		} else {

			updateByPrimaryKeySelective(role);

		}
		
		if (roleId != null  && role.getMenuIds()!= null) {
			roleMenuService.deleteRoleMenuByRoleId(roleId);
			insertRoleMenu(role);
		}
		
		result.put("roleId", roleId);

		return result;

	}
	
	/**
	 * 获取所有角色列表
	 * 
	 * @param roleId
	 * 
	 * @return RoleVo
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 */
	public RoleVo getRole(Long roleId) throws IllegalAccessException, InvocationTargetException {
		SysRole role = this.mapper.selectByPrimaryKey(roleId);
		
		RoleVo roleVo = null;
		
		if (role!=null)
		{
			roleVo = new RoleVo();
			BeanUtils.copyProperties(role,roleVo);
			
			List<RoleMenuVo> roleMenuList = roleMenuService.getRoleMenuListByRoleId(roleVo.getRoleId());
			Long[] menuIds = new Long[roleMenuList.size()];
			int i =0 ;
			for (RoleMenuVo roleMenuVo : roleMenuList)
			{
				menuIds[i] = roleMenuVo.getMenuId();
				i++;
			}
			roleVo.setMenuIds(menuIds);
		}
		
		

		return roleVo;
	}
	
	/**
     * 新增角色菜单信息
     * 
     * @param role
     * 
     * @return
     */
    public void insertRoleMenu(SysRole role)
    {
        // 新增用户与角色管理
        List<SysRoleMenu> list = new ArrayList<SysRoleMenu>();
        for (Long menuId : role.getMenuIds())
        {
            SysRoleMenu rm = new SysRoleMenu();
            rm.setRoleId(role.getRoleId());
            rm.setMenuId(menuId);
            list.add(rm);
        }
        if (list.size() > 0)
        {
        	roleMenuService.batchSave(list);
        }

    }
    
    /**
	 * 删除角色
	 * 
	 * @param roleId
	 * 
	 * @return int
	 */
	public int deleteRole(Long roleId) {
		
		if (roleId != null) {
			
			roleMenuService.deleteRoleMenuByRoleId(roleId);
			
			return this.deleteByPrimaryKey(roleId);

		}
		
		return 0;	

	}
	
	/**
	 * 启用停用角色
	 * 
	 * @param roleId
	 * @param useType
	 * 
	 * @return int
	 */
	public int useRole(Long roleId,Integer useType) {
		
		if (roleId != null) {
			SysRole role = new SysRole();
			role.setRoleId(roleId);
	        role.setUseType(useType);
			return updateByPrimaryKeySelective(role);

		}
		
		return 0;	

	}



}
